package com.htj.erp.controller;

import java.io.OutputStream;
import java.net.URLEncoder;

import javax.annotation.Resource;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.htj.erp.service.DeliveryService;
import com.htj.erp.util.PathVo;
import com.htj.erp.vo.DeliveryVO;
import com.htj.erp.vo.LocationVo;
import com.kedacom.utils.ArrayUtil;
import com.kedacom.utils.DateFormatUtil;
import com.kedacom.utils.RequestUtil;
import com.kedacom.utils.encrypt.Base64;
import com.kedacom.web.ResultMessage;
import com.kedacom.web.controller.BaseController;

/**
 * 
 * 
 * delivery的界面处理controller
 * DeliveryController
 * 继承com.kedacom.web.controller.BaseController 
 * chenghongtao
 * 2016-11-08 16:33:14
 * HTJ
 * 
 * @version 1.0.0
 *
 */
@Controller
@RequestMapping({ "/platform/delivery" })
public class DeliveryController extends BaseController {

	@Resource
	private DeliveryService deliveryService;
	
	 /**
     * 
     * page 列表页面
     * @return
     * @throws Exception
     *ModelAndView
     * @exception
     * @since  1.0.0
     */
	@RequestMapping("/page.do")
    public ModelAndView page() throws Exception {
        return getAutoView();
    }
    
    /**
	 * 
	 * listData 查询所有数据 通过界面查询
	 * @param pageable
	 * @return
	 * @throws Exception
	 *Page<DeliveryPO>
	 * @exception
	 * @since  1.0.0
	 */
    @RequestMapping("/listData.do")
    @ResponseBody
    public Page<DeliveryVO> listData(Pageable pageable) throws Exception
    {
        return deliveryService.listByPage(RequestUtil.getParameterValueMap(request, false,false), pageable);
    }
	 
	/**
	 * 编辑界面
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/editForm.do")
	public ModelAndView editForm(Long id) throws Exception
	{
		if(id== null || id==0)
		{
			return this.getAutoView();
		}
		else
		{
			return this.getAutoView().addObject("vo", this.deliveryService.getById(id));
		}
	}
	
	
	@RequestMapping("/addForm.do")
	public ModelAndView addForm(Long id) throws Exception
	{
		return this.getAutoView();
	}
	
	/**
	 * 查看界面
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/view.do")
	public ModelAndView view(Long id) throws Exception
	{
		if(id== null || id==0)
		{
			return this.getAutoView();
		}
		else
		{
			return this.getAutoView().addObject("vo", this.deliveryService.getById(id));
		}
	}
	/**
     * 
     * saveOrUpdate 保存数据
     * @param deliveryPO
     * @return
     * @throws Exception
     *ResultMessage
     * @exception
     * @since  1.0.0
     */
    @RequestMapping("/saveOrUpdate.do")
    @ResponseBody
    public ResultMessage saveOrUpdate(DeliveryVO deliveryVO) throws Exception
    {
        deliveryVO = this.deliveryService.saveOrUpdate(deliveryVO);
        return new ResultMessage(ResultMessage.Success,"保存成功",deliveryVO);
        
    }
    @RequestMapping("/submitSending.do")
    @ResponseBody
    public ResultMessage submitSending(Long id) throws Exception
    {
        this.deliveryService.submitSending(id);
        return new ResultMessage(ResultMessage.Success,"保存成功");
        
    }
    @RequestMapping("/cancelSending.do")
    @ResponseBody
    public ResultMessage cancelSending(Long id) throws Exception
    {
        this.deliveryService.cancelSending(id);
        return new ResultMessage(ResultMessage.Success,"保存成功");
        
    }
    
    
    @RequestMapping("/closeDelivery.do")
    @ResponseBody
    public ResultMessage closeDelivery(Long id) throws Exception
    {
        this.deliveryService.closeDelivery(id);
        return new ResultMessage(ResultMessage.Success,"保存成功");
        
    }
    
    
     /**
     * 
     * delById 根据ID删除
     * @param id
     * @return
     * @throws Exception
     *ResultMessage
     * @exception
     * @since  1.0.0
     */
    @RequestMapping("/delById.do")
    @ResponseBody
    public ResultMessage delById(Long id) throws Exception
    {
    	
    	
        this.deliveryService.delById(id);
        return new ResultMessage(ResultMessage.Success,"删除成功");
    }

    /**
     * 
     * delByIds 根据多个ID字符串删除
     * @param ids
     * @return
     * @throws Exception
     *ResultMessage
     * @exception
     * @since  1.0.0
     */
       
    @RequestMapping("/calSendWay.do")
    @ResponseBody
    public ResultMessage calSendWay(Long id,Boolean usingMaxDis,LocationVo start) throws Exception
    {
        PathVo vo = this.deliveryService.calSendWay(start,id,usingMaxDis);
        return new ResultMessage(ResultMessage.Success,"计算成功",vo);
    }
     
     
     @RequestMapping("/delByIds.do")
     @ResponseBody
     public ResultMessage delByIds(String ids) throws Exception
     {
         this.deliveryService.delByIds(ArrayUtil.convertArray(ids.split(",")));
         return new ResultMessage(ResultMessage.Success,"删除成功");
     }
     
     @RequestMapping("/export.do")
     public void export(Long id) throws Exception
     {
    	 OutputStream os = this.response.getOutputStream();
    	  DeliveryVO vo = this.deliveryService.getById(id);
    	  response.setContentType("APPLICATION/OCTET-STREAM");
    	  String fileName = "送货清单" + DateFormatUtil.format(vo.getSendDate(), "YYYYMMdd") + ".xls";
	       String filedisplay = fileName;
	       String agent = request.getHeader("USER-AGENT");
	       //IE11已经没有MSIE,用Gecko来判断不是IE
	       if ((agent != null) && (agent.indexOf("MSIE") == -1) && agent.indexOf("Gecko") == -1) {
	         String enableFileName = "=?UTF-8?B?" + new String(Base64.getBase64(filedisplay)) + "?=";
	         response.setHeader("Content-Disposition", "attachment; filename=" + enableFileName);
	       }
	       else {
	         filedisplay = URLEncoder.encode(filedisplay, "utf-8");
	         response.addHeader("Content-Disposition", "attachment;filename=" + filedisplay);
	       }
    	  this.deliveryService.export(id,os);
    	  os.close();
    	  os = null;
    	  response.flushBuffer();
     }
     
}
